package com.example.stray_animal_management_system.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.stray_animal_management_system.admin.mapper.UserLoginInfoMapper;
import com.example.stray_animal_management_system.admin.service.UserLoginInfoService;
import com.example.stray_animal_management_system.entity.UserLoginInfo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @description:
 * @author: panpan
 * @time: 2025/11/9
 */
@Service
public class UserLoginInfoServiceImpl extends ServiceImpl<UserLoginInfoMapper, UserLoginInfo> implements UserLoginInfoService {

    @Override
    public List<UserLoginInfo> getAllLoginInfo() {
        LambdaQueryWrapper<UserLoginInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(UserLoginInfo::getLoginTime);
        return list(wrapper);
    }

    @Override
    public boolean saveLoginInfo(UserLoginInfo loginInfo) {
        loginInfo.setLoginTime(LocalDateTime.now());
        loginInfo.setStatus(1); // 设置为在线状态
        return save(loginInfo);
    }

    @Override
    public boolean updateLogoutInfo(Long userId) {
        LambdaUpdateWrapper<UserLoginInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserLoginInfo::getUserId, userId)
                .eq(UserLoginInfo::getStatus, 1)
                .set(UserLoginInfo::getLogoutTime, LocalDateTime.now())
                .set(UserLoginInfo::getStatus, 0);
        return update(wrapper);
    }

    @Override
    public boolean deleteLoginInfo(Long id) {
        return removeById(id);
    }
}
